<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} ({{ ebill.code + '-' + ebill.name }})</span>
      </template>
      <template #toolbar>
        <a-button
          type="primary"
          @click="handleForm({ billId: ebill.id, 'extdBill.code': ebill.code })"
          v-auth="'extd:bill:view:extdBillView:edit'"
        >
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.extdBill.code + '-' + record.extdBill.name }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsExtdBillViewExtdBillViewList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    extdBillViewDelete,
    extdBillViewListData,
    extdBillViewExecSql,
  } from '/@/api/extd/bill.view/extdBillView';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('extd.bill.view.extdBillView');
  const { showMessage } = useMessage();

  const ebill = ref({
    id: ref<string>(router.currentRoute.value.query.id as string),
    code: ref<string>(router.currentRoute.value.query.code as string),
    name: ref<string>(router.currentRoute.value.query.name as string),
  });

  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('视图附表管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('数据源'),
        field: 'dataBase',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('单据标识'),
      dataIndex: 'extdBill.code',
      key: 'bill.code',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('数据源'),
      dataIndex: 'dataBase',
      key: 'a.data_base',
      sorter: true,
      width: 130,
      align: 'left',
      dictType: 'extd_data_base',
    },
    {
      title: t('表类型标识'),
      dataIndex: 'mainClassName',
      key: 'a.main_class_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('视图名'),
      dataIndex: 'viewName',
      key: 'a.view_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('层级'),
      dataIndex: 'level',
      key: 'a.level',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('回写字段'),
      dataIndex: 'reColName',
      key: 'a.re_col_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('回写值'),
      dataIndex: 'reColVal',
      key: 'a.re_col_val',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('视图代码'),
      dataIndex: 'viewSql',
      key: 'a.view_sql',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑视图附表'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'extd:bill:view:extdBillView:edit',
      },
      {
        icon: 'simple-line-icons:magic-wand',
        color: 'error',
        title: t('创建视图'),
        onClick: handleExecSql.bind(this, { id: record.id }),
        auth: 'extd:bill:view:extdBillView:edit',
      },
      {
        icon: 'simple-line-icons:link',
        title: t('编辑视图附表'),
        onClick: handleContList.bind(this, {
          id: record.id,
          code: record.extdBill.code,
          name: record.extdBill.name,
          mainClassName: record.mainClassName,
        }),
        auth: 'extd:bill:view:cont:extdBillContrast:view',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除视图附表'),
        popConfirm: {
          title: t('是否确认删除视图附表'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'extd:bill:view:extdBillView:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: extdBillViewListData,
    beforeFetch: (params) => {
      params.billId = ebill.value.id;
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: false,
    canResize: true,
    pagination: false,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await extdBillViewDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleContList(record: Recordable) {
    router.push({
      path: '/extd/bill/view/cont/extdBillContrast/list',
      query: {
        id: record.id,
        code: record.code,
        name: record.name,
        mainClassName: record.mainClassName,
      },
    });
  }

  async function handleExecSql(record: Recordable) {
    const res = await extdBillViewExecSql(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
